<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">

	$(document).ready(function(){
		function addStudentSubmit(){
			$.post("/Student/addStudent",$("#addStudentForm").serialize(), function(data,status){
				 alert("新增学生成功！");
				 window.location.href="./findStudentById.jsp";
			  });
		}
		function findStudentInfo() {
			$.ajax({
				url: "/Class/findAllClassDistinct",
				type: "get",
				dataType: "json",
				success: function(data) {
					
					var list= data;
					console.log(data);
					
					
					//取值
					$.each(list,function(index,data){
						
						$('<option value ="'+data.id+'">'+data.classname+'</option>').appendTo($("#classid"));
						
						
					});
					
					
					
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert("查找失败，找不到班级！");
					/* alert(XMLHttpRequest.status);
					alert(XMLHttpRequest.readyState);
					alert(textStatus); */
				},
			})
		}
		//点击
		$("#addStudentSubmit").click(function(){
			addStudentSubmit();
		});
		//加载
		findStudentInfo();
	});
</script>
</head>
<center>
	<form id="addStudentForm">
		
			
				编号：<input type="text" name="id"><br>
				姓名：<input type="text" name="name"><br>
				年龄：<input type="text" name="age"><br>
				性别：<select id="sex" name="sex">
						<option value ="1">男</option>
  						<option value="0">女</option>
					</select>
				班级：<select id="classid" name="classid"></select><br>
			
		<input type="button" id="addStudentSubmit" value="添加" />
	</form>
</center>
</html>